package generator;

import org.eclipse.uml2.uml.Element;
import java.util.Properties;

public class JpaEntityAssociationsOneToManyTemplateClass extends BaseTemplateClass {


	public JpaEntityAssociationsOneToManyTemplateClass(Properties properties) {
		super(properties);
	}

	public boolean isProcessable() {
		return true;
	}

	public void setElement(Element ele) {
		this.element = ele;
		returningString = new StringBuilder();
	}

	protected String _returning() {
		returningString.append("\n\t/**\n\t * JPA configuration for the one-to-many relationship with ");
		returningString.append(getOppositeType());
		returningString.append(".\n\t */\n\t@OneToMany(cascade = CascadeType.ALL, mappedBy = \"");
		returningString.append(getOppositeRole());
		returningString.append("\", fetch = FetchType.LAZY)\n\tprivate java.util.List<");
		returningString.append(getOppositeType());
		returningString.append("> ");
		returningString.append(getOppositeRole());
		returningString.append(";\n\n\t/**\n\t * Return a list of ");
		returningString.append(getOppositeRole());
		returningString.append(" related to te current instance.\n\t */\n\tpublic java.util.List<");
		returningString.append(getOppositeType());
		returningString.append("> get");
		returningString.append(getMethodName());
		returningString.append("() {\n\t\treturn this.");
		returningString.append(getOppositeRole());
		returningString.append(";\n\t}\n\t\n\t/**\n\t * Allows modify the list of ");
		returningString.append(getOppositeRole());
		returningString.append(" associated to the current instance.\n\t */\n\tpublic void set");
		returningString.append(getMethodName());
		returningString.append("(java.util.List<");
		returningString.append(getOppositeType());
		returningString.append("> _");
		returningString.append(getOppositeRole());
		returningString.append(") {\n\t\tthis.");
		returningString.append(getOppositeRole());
		returningString.append(" = _");
		returningString.append(getOppositeRole());
		returningString.append(";\n\t}\n\t");

		return returningString.toString();
	}
	private String oppositeRole;
	
	private String getOppositeRole() {
		return oppositeRole;
	}
	
	private String oppositeType;
	
	private String getOppositeType() {
		return oppositeType;
	}
	
	private String methodName;
	
	private String getMethodName() {
		return methodName;
	}
	
	/**
	 * Dada una clase, retorna todas las asociaciones en las que juega algún
	 * rol.
	 * 
	 * @return
	 */
	public String returning() {
		StringBuilder sb = new StringBuilder();
		for (org.eclipse.uml2.uml.Relationship r : element.getRelationships()) {
			if (r instanceof org.eclipse.uml2.uml.Association) {
				utils.AssociationInfo ai = utils.AssociationInfo.getInfo((org.eclipse.uml2.uml.Association)r);
				utils.AssociationInfo.Side oppositeSide = ai.getOppositeSide((org.eclipse.uml2.uml.Class) element);
				if (oppositeSide.isMany()) {
					oppositeRole = oppositeSide.getRole();
					oppositeType = oppositeSide.getType();
					methodName = firstUpper(oppositeSide.getRole());
					sb.append(_returning());
				}
			}
		}
		return sb.toString();
	}
	
}